layui.use(['table','form','jquery','treetable','notice'],function () {
    let table = layui.table;
    let form = layui.form;
    let $ = layui.jquery;
    let notice = layui.notice;
    let treetable = layui.treetable;
    window.render = function() {
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            skin: 'line',
            method: 'post',
            treeDefaultClose: true,
            toolbar: '#category-toolbar',
            elem: '#category-table',
            url: '/admin.php/articlecategory/index',
            page: false,
            cols: [
                [
                    {type: 'checkbox'},
                    {field: 'name', minWidth: 200, title: '分类名称', align: 'center'},
                    {field: 'status', title: '是否可用', templet: '#category-enable', align: 'center'},
                    {field: 'sort', title: '排序', align: 'center'},
                    {field: 'create_time', title: '创建时间', align: 'center'},
                    {title: '操作', templet: '#category-bar', width: 150, align: 'center'}
                ]
            ]
        });
    }
    render();

    table.on('tool(category-table)',function(obj){
        if (obj.event === 'remove') {
            window.remove(obj);
        } else if (obj.event === 'edit') {
            window.edit(obj);
        }
    })

    table.on('toolbar(category-table)', function(obj){
        if(obj.event === 'add'){
            window.add();
        } else if(obj.event === 'refresh'){
            window.refresh();
        } else if(obj.event === 'batchRemove'){
            window.batchRemove(obj);
        } else if(obj.event === 'expandAll'){
            treetable.expandAll("#category-table");
        } else if(obj.event === 'foldAll'){
            treetable.foldAll("#category-table");
        }
    });

    form.on('switch(category-enable)', function(data) {
        var status = data.elem.checked?1:0;
        var id = this.value;
        $.post('/admin.php/articlecategory/status',{status:status,id:id},function (result) {
            if (result.code){
                notice.success(result.msg)
            } else {
                notice.error(result.msg)
            }
        })
    });

    if (typeof width !== 'number' || width === 0) {
        width = $(window).width();
    }
    if (typeof height !== 'number' || height === 0) {
        height = $(window).height();
    }

    window.add = function() {
        layer.open({
            type: 2,
            scrollbar: true,
            title: '新增',
            shade: false,
            resize:true,
            maxmin:true,
            isOutAnim:false,
            anim:2,
            area: [width + 'px', height + 'px'],
            content:  '/admin.php/articlecategory/add',
            end:function () {
                render();
            }
        });
    }

    window.edit = function(obj) {
        layer.open({
            type: 2,
            scrollbar: true,
            title: '修改',
            shade: false,
            resize:true,
            maxmin:true,
            isOutAnim:false,
            anim:2,
            area: [width + 'px', height + 'px'],
            content: '/admin.php/articlecategory/edit'
        });
    }

    window.remove = function(obj) {
        layer.confirm('确定要删除该分类', {
            icon: 3,
            title: '提示'
        }, function(index) {
            layer.close(index);
            $.ajax({
                url:'/admin.php/articlecategory/del',
                data:{id:obj.data['id']},
                type: 'POST',
                success: function(result) {
                    if (result.code) {
                        notice.success(result.msg)
                        obj.del();
                    } else {
                        notice.error(result.msg)
                    }
                }
            })
        });
    }

    window.batchRemove = function(obj) {
        let data = table.checkStatus(obj.config.id).data;
        if (data.length === 0) {
            notice.warning("未选数据");
            return false;
        }
        let ids = "";
        for (let i = 0; i < data.length; i++) {
            ids += data[i].id + ",";
        }
        ids = ids.substr(0, ids.length - 1);
        layer.confirm('确定删除这些数据', {
            icon: 3,
            title: '提示'
        }, function(index) {
            layer.close(index);
            $.ajax({
                url:  "/admin.php/articlecategory/delAll",
                data:{ids:ids},
                type: 'post',
                success: function(result) {
                    if (result.code) {
                        notice.success(result.msg)
                        render();
                    } else {
                        notice.error(result.msg)
                    }
                }
            })
        });
    }
})